package com.example.treehole.common;

import org.springframework.http.ResponseEntity;

/**
 * 通用的响应结果类，用于封装接口的返回数据。
 */
public class BaseResult<T> {
    // 响应状态码
    private int code;
    // 响应消息
    private String msg;
    // 响应的具体数据
    private T data;


    public BaseResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public static <D> ResponseEntity<BaseResult<D>> ok(D data) {
        return ResponseEntity.ok(new BaseResult<>(200, "成功", data));
    }


    public static <D> ResponseEntity<BaseResult<D>> ok(String msg, D data) {
        return ResponseEntity.ok(new BaseResult<>(200, msg, data));
    }


    public static <D> ResponseEntity<BaseResult<D>> ok(String msg) {
        return ResponseEntity.ok(new BaseResult<>(200, msg, null));
    }


    public static <D> ResponseEntity<BaseResult<D>> error(String msg) {
        return ResponseEntity.ok(new BaseResult<>(201, msg, null));
    }

    // 获取状态码
    public int getCode() {
        return code;
    }

    // 设置状态码
    public void setCode(int code) {
        this.code = code;
    }

    // 获取消息
    public String getMsg() {
        return msg;
    }

    // 设置消息
    public void setMsg(String msg) {
        this.msg = msg;
    }

    // 获取具体数据
    public T getData() {
        return data;
    }

    // 设置具体数据
    public void setData(T data) {
        this.data = data;
    }
}
